<template>
  <div class="container">
    <div class="search-container">
      <div class="input-box">
        <van-search
          v-model="name"
          placeholder="请输入客户姓名"
          @input="onSearch"
        />
      </div>
      <img
        @click="showFilter = !showFilter"
        src="@/assets/images/customerInfo/filter-fill.png"
        alt=""
      />
    </div>
    <div
      class="item"
      v-for="(item, index) in list"
      :key="index"
      @click="goDetail(item.id)"
    >
      <div class="left">
        <div class="title">
          <span>姓名：</span>
          <span>{{ item.name }}</span>
        </div>
        <div class="title">
          <span>身份证号：</span>
          <span>{{ item.idcardNo }}</span>
        </div>
        <div class="mobile">
          <span>手机号：</span>
          <a :href="'tel:' + item.phone" @click.stop="">{{ item.phone }}</a>
        </div>
        <div class="time">
          <span>分配时间：</span>
          <span>{{ item.distributeTime }}</span>
        </div>
        <!-- <div class="time">
          <span>客户类型：</span>
          <span>{{ CUSTOMERTYPE[item.customerType] }}</span>
        </div> -->
      </div>
      <div class="right">
        <img src="" alt="" />
      </div>
    </div>
    <div class="load-more" v-if="!isEnd && !isEmpty">
      <van-button
        :loading="isLoading"
        loading-text="加载中..."
        type="default"
        @click="loadMore"
        >加载更多...</van-button
      >
    </div>
    <div class="no-data" v-if="isEnd && !isEmpty">
      <van-button plain type="default">没有更多了...</van-button>
    </div>
    <div class="fixed">
      <van-button type="primary" @click="goAddCustomer"
        >添加意向客户</van-button
      >
    </div>
    <div v-if="isEnd && isEmpty" class="no-content">当前内容为空~</div>
    <filter-modal
      :visible="showFilter"
      @close="showFilter = false"
      @onOk="onOk"
    ></filter-modal>
  </div>
</template>

<script>
import { getList } from "@/api/customer";
import { pagination, CUSTOMERTYPE } from "@/utils/enumConfig";
import _ from "lodash";
import FilterModal from "./components/filter-modal.vue";

export default {
  components: {
    FilterModal,
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      // 回到原来的位置
      const position = JSON.parse(window.sessionStorage.getItem("position"));
      document.documentElement.scrollTop = position;
    });
  },
  /* 页面离开时,修改keepAlive值为false，保证进入该页面时页面刷新
   * 记录离开前页面滚动条位置
   */
  beforeRouteLeave(to, from, next) {
    from.meta.keepAlive = false;
    const position = document.documentElement.scrollTop;
    window.sessionStorage.setItem("position", JSON.stringify(position));
    next();
  },
  data() {
    return {
      CUSTOMERTYPE: CUSTOMERTYPE,
      pagination: this.$_.cloneDeep(pagination),
      list: [],
      isEnd: false,
      isEmpty: false,
      isLoading: false,
      name: "",
      showFilter: false,
    };
  },
  async mounted() {
    await this.getList(this.pagination);
  },
  methods: {
    async loadMore() {
      let { current } = this.pagination;
      await this.getList({
        ...this.pagination,
        current: current + 1,
      });
    },
    async getList(pagination, params = {}) {
      this.isLoading = true;
      let { size } = pagination;
      let list = await getList({ ...params, ...pagination });
      if (list) {
        this.list = this.list.concat(list);
        this.pagination = pagination;
        this.isEnd = list.length < size ? true : false;
        this.isEmpty = this.list.length === 0 ? true : false;
      }
      this.isLoading = false;
    },
    goAddCustomer() {
      this.$router.push("/customerInfo/add");
    },
    goDetail(id) {
      this.$router.push(`/customerInfo/add?id=${id}`);
    },
    onSearch: _.debounce(function() {
      this.mySearch();
    }, 800),
    mySearch() {
      this.list = [];
      this.getList({ ...this.$_.cloneDeep(pagination), name: this.name });
    },
    async onOk(values) {
      this.list = [];
      await this.getList(
        {
          ...this.pagination,
          current: 1,
        },
        { ...values }
      );
      this.showFilter = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.search-container {
  display: flex;
  width: 100%;
  background: #fff;
  align-items: center;
  .input-box {
    width: 90%;
  }
  img {
    width: 24px;
    height: 28px;
    // object-fit: cover;
  }
}
.item {
  width: 100%;
  background: #fff;
  padding: 20px 0 0;
  color: #333;
  font-size: 14px;
  border-bottom: 1px solid #f1f1f1;
  margin-top: 10px;
  .left {
    margin-left: 20px;
  }
}
.load-more,
.no-data {
  width: 100%;
  button {
    width: 100%;
    color: #666;
  }
}
.fixed {
  position: fixed;
  bottom: 0;
  width: 100%;
  button {
    width: 100%;
  }
}
.container {
  padding-bottom: 20%;
}
.no-content {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #888;
}
</style>
